package cn.buk.tms.eotms.controller;

import cn.buk.common.JsonResult;
import cn.buk.common.dto.CommonDto;
import cn.buk.tms.common.dto.base.BaseDto;
import cn.buk.tms.eotms.dto.CarOrderDto;
import cn.buk.tms.eotms.sc.SearchCarOrderCriteria;
import cn.buk.tms.eotms.service.RentalCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * 租车服务，调用接口
 * @author yfdai
 */
@RestController
@RequestMapping("/rental")
public class RentalCarController extends BaseController {

  private final RentalCarService rentalCarService;

  @Autowired
  public RentalCarController(RentalCarService rentalCarService) {
    this.rentalCarService = rentalCarService;
  }

  /**
   * 提交订单
   */
  @RequestMapping(value = "/car/order", method = POST)
  public BaseDto submitRentalCarOrder(HttpServletRequest request,
                                      @RequestParam("orderNo") String orderNo,
                                      @RequestParam("vehicleTypeId") int vehicleTypeId,
                                      @RequestParam("vehicleTypeName") String vehicleTypeName,
                                      @RequestParam("price") double price,
                                      @RequestParam("discount") double discount,
                                      @RequestParam("linkman") String linkman,
                                      @RequestParam("linkPhone") String linkPhone,
                                      @RequestParam(value = "email", required = false) String email,
                                      @RequestParam(value = "payType", defaultValue = "8") int payType,
                                      @RequestParam("remark") String remark,
                                      @RequestParam(value = "flightOrderId", defaultValue = "0") int flightOrderId
  ) {
    return rentalCarService.submitCarOrder(getUserId(request), orderNo, vehicleTypeId, vehicleTypeName, price, discount, linkPhone, linkman, email, payType, remark, flightOrderId);
  }

  /**
   * 查找订单
   */
  @RequestMapping("/car/orders")
  public CommonDto<CarOrderDto> searchRentalCarOrders(HttpServletRequest request,
      @RequestParam(value = "pageNo", defaultValue = "1") final int pageNo,
      @RequestParam(value = "pageSize", defaultValue = "20") final int pageSize,
      @RequestParam(value = "orderNo", required = false) String orderNo,
      @RequestParam(value = "status", defaultValue = "-1") int status,
      @RequestParam(value = "mobile", required = false) String mobile,
      @RequestParam(value = "linkman", required = false) String linkman,
      @RequestParam(value = "useDate", required = false) Date useDate
  ) {
    final int userId = getUserId(request);

    SearchCarOrderCriteria sc = new SearchCarOrderCriteria();
    sc.setPageNo(pageNo);
    sc.setPageSize(pageSize);
    sc.setOrderNo(orderNo);
    sc.setStatus(status);
    sc.setMobile(mobile);
    sc.setName(linkman);
    sc.setDdate(useDate);

    List<CarOrderDto> datas = rentalCarService.searchRentalCarOrders(userId, sc);

    CommonDto<CarOrderDto> commonDto = new CommonDto<>();
    commonDto.setDataList(datas);
    commonDto.setPage(sc.getPage());

    return commonDto;
  }

  /**
   * 查找订单详情
   */
  @RequestMapping("/car/orders/{id}")
  public CarOrderDto searchRentalCarOrder(HttpServletRequest request,
                                                @PathVariable("id") int orderId
  ) {
    final int userId = getUserId(request);

    return rentalCarService.searchRentalCarOrder(userId, orderId);
  }

  /**
   * 取消订单
   */
  @RequestMapping("/car/orders/{id}/cancel")
  public JsonResult cancelRentalCarOrder(HttpServletRequest request,
                                   @PathVariable("id") int orderId) {
    final int userId = getUserId(request);

    return rentalCarService.cancelRentalCarOrder(userId, orderId);
  }

  /**
   * 审批订单
   * @param denyCode 0-同意；其它-不同意
   * @param denyReason 不同意的原因，不同意时必填
   */
  @PostMapping(value = {"/car/order/{id}/approve", "/order/{id}/approve"})
  public JsonResult approveRentalCarOrder(HttpServletRequest request,
                                       @PathVariable("id") int orderId,
                                       @RequestParam("denyCode") int denyCode,
                                       @RequestParam(value = "denyReason", required = false) String denyReason
  ) {
    return rentalCarService.approveRentalCarOrder(getUserId(request), orderId, denyCode, denyReason);
  }

  /**
   * 为租车订单付款
   */
  @PostMapping(value = "/car/order/{id}/pay")
  public JsonResult payRentalCarOrder(HttpServletRequest request,
                                  @PathVariable("id") int id
  ) {
    return rentalCarService.payRentalCarOrder(getUserId(request), id);
  }
}
